﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace BDSim
{
    //  [add by [Penghaijun] 2015.1.16
    /// <summary>
    /// PlaneMap.xaml 的交互逻辑
    /// </summary>
    public partial class PlaneMapView : UserControl
    {
        public PlaneMapView()
        {
            InitializeComponent();
        }

        #region 成员定义

        /// <summary>
        /// 颜色字典 
        /// </summary>
        List<Brush> DicColor = new List<Brush>()
        {
            {new SolidColorBrush(Color.FromRgb(230,60,0))},
            {new SolidColorBrush(Color.FromRgb(255,141,0))},
            {new SolidColorBrush(Color.FromRgb(255,255,0))},
            {new SolidColorBrush(Color.FromRgb(192,255,0))},
            {new SolidColorBrush(Color.FromRgb(61,249,61))},
            {new SolidColorBrush(Color.FromRgb(33,204,20))},
        };

        /// <summary>
        /// 倒序颜色字典 
        /// </summary>
        List<Brush> ReverseDicColor = new List<Brush>()
        {
            {new SolidColorBrush(Color.FromRgb(33,204,20))},
            {new SolidColorBrush(Color.FromRgb(61,249,61))},
            {new SolidColorBrush(Color.FromRgb(192,255,0))},
            {new SolidColorBrush(Color.FromRgb(255,255,0))},
            {new SolidColorBrush(Color.FromRgb(255,141,0))},
            {new SolidColorBrush(Color.FromRgb(230,60,0))},
        };

        /// <summary>
        /// 网格列数
        /// </summary>
        int gridColNum = 0;//网格列数        

        /// <summary>
        /// 网格行数 
        /// </summary>
        int gridRowNum = 0;//网格行数 
     
        /// <summary>
        /// 网格的集合
        /// </summary>
        Grid[,] gridList;//Grid的集合

        double lonSpace = 5.0;
        /// <summary>
        /// 经度间距  
        /// </summary>
        public double LonSpace
        {
            set { lonSpace = value; }
        }

        double latSpace = 2.5;
        /// <summary>
        /// 纬度间距 
        /// </summary>
        public double LatSpace
        {
            set { latSpace = value; }
        }
        #endregion

        #region 初始化绘制网格
        /// <summary>
        /// 绘制网格   
        /// </summary>
        /// <param name="rowNum">网格行数</param>
        /// <param name="colNum">网格列数</param>       
        public void Init(int rowNum,int colNum)
        {
            gridRowNum = rowNum;//网格行数
            gridColNum = colNum;//网格列数 
             
            gridList = new Grid[rowNum, colNum];
            myGrid.Children.Clear();
            //清除网格
            myGrid.ColumnDefinitions.Clear();
            myGrid.RowDefinitions.Clear();

            //绘制网格的行 
            for (int j = 0; j < gridRowNum; j++)
            {
                RowDefinition row = new RowDefinition();
                myGrid.RowDefinitions.Add(row);
            }

            //绘制网格的列 
            for (int i = 0; i < gridColNum; i++)
            {
                ColumnDefinition col = new ColumnDefinition();
                myGrid.ColumnDefinitions.Add(col);
            }

            //在每个网格中添加Grid          
            for (int i = 0; i < gridRowNum; i++)
            {
                for (int j = 0; j < gridColNum; j++)
                {
                    gridList[i, j] = new Grid();

                    Grid.SetRow(gridList[i, j], i);
                    Grid.SetColumn(gridList[i, j], j);
                    myGrid.Children.Add(gridList[i, j]);
                }
            }          
        }
        #endregion

        #region 填充颜色(填充全部网格)
        /// <summary>
        /// 填充颜色(填充全部网格)
        /// </summary>
        /// <param name="colorValue">经纬度处的颜色值</param>
        public void FillColors(int[,] colorValue, DopEnum dopType)
        {
            int lonNum = colorValue.GetLength(0);//经度数目
            int latNum = colorValue.GetLength(1);//纬度数目

            int rowNum = latNum;//行数
            int colNum = lonNum;//列数

            //add by [lijing] 2016.01.14(为了保证较好的性能都显示为绿色)
            List<Brush> tempColorDic;
            if (dopType == DopEnum.Coverage)
            {
                tempColorDic = DicColor;
            }
            else
            {
                tempColorDic = ReverseDicColor;
            }
            //add by [lijing] 2016.01.14

            if (rowNum == gridRowNum && colNum == gridColNum)//如果经度、纬度数目与网格的行数、列数相等，就填充颜色
            {
                //填充颜色
                for (int i = 0; i < colNum; i++)
                {
                    for (int j = 0; j < rowNum; j++)
                    {
                        //gridList[j, i].Background = DicColor[colorValue[i, j]]; // update by [lijing] 2016.01.14
                        //if (colorValue[i, j] >= 0 && colorValue[i, j] <= 5)
                        //{
                        //    gridList[j, i].Background = tempColorDic[colorValue[i, j]];
                        //}
                        //else
                        //{
                        //    gridList[j, i].Background = Brushes.Transparent;
                        //}
                        switch (colorValue[i, j])
                        {
                            case 0:
                            case 1:
                            case 2:
                            case 3:
                            case 4:
                            case 5:
                                gridList[j, i].Background = tempColorDic[colorValue[i, j]];
                                break;
                            default:
                                gridList[j, i].Background = Brushes.Transparent;
                                break;
                        }
                    }
                }
            }
            else//如果经度、纬度数目与网格的行数、列数不相等，就退出
            {
                return;
            }
        }
        #endregion

        #region 填充颜色（填充部分网格）
        /// <summary>
        /// 填充颜色（填充部分网格）
        /// </summary>
        /// <param name="colorValue">经纬度处的颜色值</param>
        /// <param name="rowBegin">开始填充颜色的行数</param>
        public void FillColors(int[,] colorValue, int rowBegin)
        {
            int lonNum = colorValue.GetLength(0);//经度数目
            int latNum = colorValue.GetLength(1);//纬度数目

            int rowNum = latNum;//行数
            int colNum = lonNum;//列数

            if (rowNum <= gridRowNum)//如果纬度数目小于网格的行数，就填充颜色
            {
                int rowEnd = rowNum - (rowBegin + 1);

                for (int i = 0; i < colNum; i++)
                {
                    for (int j = 0; j < rowNum; j++)
                    {
                        if (j >= rowBegin && j < rowEnd)
                        {
                            gridList[j, i].Background = DicColor[colorValue[i,j - rowBegin]];
                        }
                        else
                        {
                            gridList[j, i].Background = DicColor[6];
                        }

                    }
                }

                //填充颜色
                //for (int i = 0; i < colNum; i++)
                //{
                //    for (int j = rowBegin; j < rowEnd; j++)
                //    {
                //        gridList[j, i].Background = DicColor[colorValue[i, j - rowBegin]];
                //    }

                //    for (int j = 0; j < rowBegin; j++)//不填充颜色的地方刷成透明色 
                //    {
                //        gridList[j, i].Background = DicColor[6]; 
                //    }

                //    for (int j = rowEnd; j < rowNum; j++)//不填充颜色的地方刷成透明色 
                //    {
                //        gridList[j, i].Background = DicColor[6];
                //    }
                //}                                
            }
            else//如果经度、纬度数目与网格的行数、列数不相等，就退出
            {
                return;
            }
        }
        #endregion

        #region 填充颜色，填充部分网格
        /// <summary>
        /// 填充颜色，填充部分网格,由latBegin决定
        /// </summary>
        /// <param name="colorValue"></param>
        /// <param name="latBegin"></param>
        public void FillColors(int[,] colorValue, double latBegin)
        {
            int lonNum = colorValue.GetLength(0);//经度数目
            int latNum = colorValue.GetLength(1);//纬度数目

            int rowNum = latNum;//行数
            int colNum = lonNum;//列数

            int row = 0;
            int col = 0;

            if (rowNum <= gridRowNum)//如果纬度数目小于网格的行数，就填充颜色
            {
                //全部填充为透明的颜色
                for (double B = 180.0; B > -180.0; B -= lonSpace)
                {
                    for (double L = 90.0; L > -90.0; L -= latSpace)
                    {                        
                        col = (int)((180.0 - B) / lonSpace);
                        row = (int)((90.0 - L) / latSpace);

                        int rowBeginIndex = (int)((90-latBegin-0.000000001) / latSpace);
                        int rowEndIndex = (int)((90 + latBegin) / latSpace);

                        if (row>rowBeginIndex && row<rowEndIndex)
                        {
                            gridList[row, col].Background = DicColor[colorValue[col, row]];
                        }
                        else
                        {
                            gridList[row, col].Background = Brushes.Transparent;
                        }                      
                    }
                }

                //填充选择的纬度范围
                //for (double B = 180.0; B > -180.0; B -= lonSpace)
                //{
                //    for (double L = latBegin; L > -latBegin; L -= latSpace)
                //    {
                //        col = (int)((180.0 - B) / lonSpace);
                //        row = (int)((90.0 - L) / latSpace);

                //        gridList[row, col].Background = DicColor[colorValue[col, row]];
                //    }
                //}

                    //for (int i = 0; i < colNum; i++)
                    //{
                    //    for (int j = 0; j < rowNum; j++)
                    //    {
                    //        if (j >= rowBegin && j < rowEnd)
                    //        {
                    //            gridList[j, i].Background = DicColor[colorValue[i, j - rowBegin]];
                    //        }
                    //        else
                    //        {
                    //            gridList[j, i].Background = DicColor[6];
                    //        }

                    //    }
                    //}                                           
            }
            else//如果经度、纬度数目与网格的行数、列数不相等，就退出
            {
                return;
            }
        }
        #endregion

        #region 填充颜色（填充部分网格）
        /// <summary>
        /// 填充颜色（填充部分网格） 
        /// </summary>
        /// <param name="colorValue">经纬度处的可见卫星数目</param>
        /// <param name="rowBegin">开始填充颜色的行数</param>
        /// <param name="colBegin">开始填充颜色的列数</param>
        public void FillColors(int[,] colorValue, int rowBegin, int colBegin)
        {
            int lonNum = colorValue.GetLength(0);//经度数目
            int latNum = colorValue.GetLength(1);//纬度数目

            int rowNum = latNum;//行数
            int colNum = lonNum;//列数

            if (rowNum <= gridRowNum && colNum <= gridColNum)//如果经度、纬度数目与网格的行数、列数相等，就填充颜色
            {
                //int[,] iColorValue = ColorRank(colorValue);//颜色等级分类

                int rowEnd = rowNum - (rowBegin + 1);
                int colEnd = colNum - (colBegin + 1);

                for (int i = 0; i < colNum; i++)
                {
                    for (int j = 0; j < rowNum; j++)
                    {
                        if (i >= colBegin && i < colEnd && j >= rowBegin && j < rowEnd)
                        {
                            gridList[j, i].Background = DicColor[colorValue[i - colBegin, j - rowBegin]];
                        }
                        else
                        {
                            gridList[j, i].Background = DicColor[6];
                        }
                        
                    }
                }              
            }
            else//如果经度、纬度数目与网格的行数、列数不相等，就退出
            {
                return;
            }
        }
        #endregion

        #region 颜色等级分类
        /// <summary>
        /// 颜色等级分类
        /// </summary>
        /// <param name="colorValue">经纬度处的可见卫星数目</param>
        /// <returns>可见卫星数目对应的颜色值</returns>
        int[,] ColorRank(int[,] colorValue)
        {
            int rowNum = colorValue.GetLength(0);//行数
            int colNum = colorValue.GetLength(1);//列数

            int maxNum = colorValue[0, 0];//可见卫星数目中最大的数
            int minNum = colorValue[0, 0];//可见卫星数目中最小的数

            //获取可见卫星数目中的最大值和最小值
            for (int i = 0; i < rowNum; i++)
            {
                for (int j = 0; j < colNum; j++)
                {
                    int var = colorValue[i, j];
                    maxNum = var > maxNum ? var : maxNum;//可见卫星数目中最大的数
                    minNum = var > minNum ? minNum : var;//可见卫星数目中最小的数
                }
            }

            int colorType = DicColor.Count;//颜色字典中，颜色的种类
            int midPart =(int) (maxNum - minNum) / colorType;

            int[,] iColorValue = new int[rowNum, colNum];

            //将可见卫星数目转化成相应的颜色字典中的颜色
            for (int i = 0; i < rowNum; i++)
            {
                for (int j = 0; j < colNum; j++)
                {
                    iColorValue[i, j] = (int)((colorValue[i, j] - minNum) / midPart);
                    if (iColorValue[i, j] >= colorType)
                    {
                        iColorValue[i, j] = colorType - 1;
                    }
                }
            }
            return iColorValue;
        }
        #endregion
    }
}
